package com.ce.pms.customer.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.hibernate.Criteria;
import org.hibernate.Query;

import com.ce.pms.customer.dao.IOwnerDao;
import com.ce.pms.customer.model.ClientDO;
import com.ce.pms.customer.model.ParticipantDO;
import com.ce.pms.customer.model.ParticipantRoleDO;
import com.ce.pms.house.dao.IFlatDao;
import com.ce.pms.house.model.CoalitionHouseDomain;
import com.ce.pms.house.model.HouseDomain;
import com.ce.util.BlankUtil;
import com.sitechasia.webx.core.support.Page;

/**
 * 功能描述: OwnerDaoImpl
 * <p>版权所有：中企动力
 * <p>未经本公司许可，不得以任何方式复制或使用本程序任何部分
 *
 * @author  付湘辉
 * Created on 2008-05-22
 * @author  付湘辉
 * update on：  2008-6-2
 * Why:修改方法　queryClientRHouseListByPage
 */
public class OwnerDaoImpl extends CustomerDaoImpl implements IOwnerDao {
	private IFlatDao flatDao;

	/**
	 *
	 * 功能描述: 得到管理房下所有产权房屋信息。
	 *
	 * @author 付湘辉
	 * Created on:2008-6-12
	 *
	 * 修改原因：
	 * @param houseDomain
	 * @return List<HouseDomain> 返回结果
	 */
	public List<HouseDomain> getAllHouseInCoalitionHouse(HouseDomain houseDomain){

		List<HouseDomain> houseList = new ArrayList<HouseDomain>();
		//得到有效合并房实体
		CoalitionHouseDomain coalitionHouse = flatDao.getShareProRightCoalition(houseDomain);

		if (BlankUtil.isBlank(coalitionHouse)) {
			houseList.add(houseDomain);
			return houseList;
		}

		Set<HouseDomain> houseSet = coalitionHouse.getHouses();

		if (!BlankUtil.isBlank(houseSet)) {
			//得到此合并房下的所有产权房屋信息
			for (HouseDomain houseDO2 : houseSet) {
				houseList.add(houseDO2);
			}
		}

		return houseList;
	}

	/**
	 *
	 * 功能描述: 通过房号查询相应房屋，此功能应属于房产模块，因未做，暂时处理
	 *
	 * @author 付湘辉
	 * Created on:2008-6-6
	 *
	 * 修改原因：
	 * @param houseNumber
	 * @return HouseDomain 返回结果
	 */
	public HouseDomain getHouseByHouseNumber(String houseNumber){
		HouseDomain houseDomain =null;

		StringBuilder sb = new StringBuilder();

		if (BlankUtil.isBlank(houseNumber)) {
			return null;
		}

		sb.append("FROM HouseDomain AS hd WHERE hd.houseNumber = ? ");

		Query query = super.createQuery(sb.toString(), String.valueOf(houseNumber));

		List list = query.list();

		if (list.size()>0) {
			houseDomain = (HouseDomain)list.get(0);
		}

		return houseDomain;
	}


	/**
	 * 功能描述: 通过参与人ID查询得到客户房屋关系的列表
	 *
	 * @author 付湘辉
	 * Created on:　2008-05-22
	 * update on：  2008-6-2
	 * 修改原因： 修改方法注释
	 * @param participantId
	 * @param pageNo
	 * @param pageSize
	 * @roseuid 483117990196
	 * @return Page 返回结果
	 */
	public Page queryClientRHouseListByPage(Long participantId,
											Integer pageNo,
											Integer pageSize) {
		Page page = new Page();

		ParticipantDO participantDO =  findById(participantId);

		Criteria criteria = criteriaFindUniqueBy("participant", participantDO);

		ParticipantRoleDO participantRoleDO = (ParticipantRoleDO) findUnique(criteria);

		ClientDO clientDO = null;

		String hql = "from ClientRHouseDO as c where c.client=? and c.validity='1'";

		page = pagedQueryByPageNo(hql, pageNo, pageSize,
				new Object[] { clientDO });

		return page;
	}


	public void setFlatDao(IFlatDao flatDao) {
		this.flatDao = flatDao;
	}


}
